//Page Object
Page({
    data: {
        tabs:[
            {
              id:0,
              value:"音乐推荐",
              isActive:true
            },
            {
              id:1,
              value:"播放器",
              isActive:false
            },
            {
              id:2,
              value:"播放列表",
              isActive:false
            }
        ],
        // 热门歌曲信息
        infolist: [{
            id: 0,
            title: "赤伶",
            src:"https://p2.music.126.net/0el5yaTEo3KjeJjL3ZXxmg==/109951165054951989.jpg"
        }, {
            id: 1,
            title: "起风了",
            src: "https://p2.music.126.net/diGAyEmpymX8G7JcnElncQ==/109951163699673355.jpg"
        },{
            id: 2,
            title: "蜂鸟",
            src:"https://p1.music.126.net/rQw-f1jS2PIDnY2DoYlO7w==/109951163890222255.jpg"
        },{
            id: 3,
            title: "后来",
            src:"	https://p1.music.126.net/eBF7bHnJYBUfOFrJ_7SUfw==/109951163351825356.jpg"
        }, {
            id: 4,
            title: "童话",
            src: "http://p1.music.126.net/n5vJ7BA7bdnlpI_0giRxHA==/109951165683825147.jpg"
        },{
            id: 5,
            title: "光年之外",
            src:"http://p2.music.126.net/fkqFqMaEt0CzxYS-0NpCog==/18587244069235039.jpg"
        }, {
            id: 6,
            title: "情歌王",
            src:"https://p1.music.126.net/iSgf-6zYJqMXB-WKlF6nbw==/109951165304540603.jpg"
        }],
        index: 0,
        tab: 0,
        // 播放列表数据
        playlist: [{
            id: 1,
            title: '赤伶',
            singer: '李玉刚',
            src: 'http://music.163.com/song/media/outer/url?id=1454730043.mp3',
            coverImgUrl: 'https://p2.music.126.net/0el5yaTEo3KjeJjL3ZXxmg==/109951165054951989.jpg'
        }, {
            id: 2,
            title: '起风了',
            singer: '买辣椒也用券',
            src: 'http://music.163.com/song/media/outer/url?id=1330348068.mp3',
            coverImgUrl: 'https://p2.music.126.net/diGAyEmpymX8G7JcnElncQ==/109951163699673355.jpg'
        }, {
            id: 3,
            title: '蜂鸟',
            singer: '吴青峰',
            src: 'http://music.163.com/song/media/outer/url?id=1348568908.mp3',
            coverImgUrl: 'https://p1.music.126.net/rQw-f1jS2PIDnY2DoYlO7w==/109951163890222255.jpg'
        }, {
            id: 4,
            title: '后来',
            singer: '刘若英',
            src: 'http://music.163.com/song/media/outer/url?id=254574.mp3',
            coverImgUrl: 'https://p1.music.126.net/eBF7bHnJYBUfOFrJ_7SUfw==/109951163351825356.jpg'
        }, {
            id: 5,
            title: '童话',
            singer: '光良',
            src: 'http://music.163.com/song/media/outer/url?id=85580.mp3',
            coverImgUrl: 'http://p1.music.126.net/n5vJ7BA7bdnlpI_0giRxHA==/109951165683825147.jpg'
        },{
            id: 6,
            title: '光年之外',
            singer: 'G.E.M.邓紫棋',
            src: 'http://music.163.com/song/media/outer/url?id=449818741.mp3',
            coverImgUrl: 'http://p2.music.126.net/fkqFqMaEt0CzxYS-0NpCog==/18587244069235039.jpg'
        },{
            id: 7,
            title: '情歌王',
            singer: '古巨基',
            src: 'http://music.163.com/song/media/outer/url?id=407002231.mp3',
            coverImgUrl: 'https://p1.music.126.net/iSgf-6zYJqMXB-WKlF6nbw==/109951165304540603.jpg'
        }],
        state: '暂停中',
        playIndex: 0,
        play: {
            currentTime: '00:00',
            duration: '00:00',
            percent: 0,
            title: '',
            singer: '',
            coverImgUrl: 'https://p2.music.126.net/0el5yaTEo3KjeJjL3ZXxmg==/109951165054951989.jpg',
        }
    },
   // 页面切换
    changeItem: function (e) {
        const { index } = e.target.dataset
         //修改源数组
         let {tabs} =this.data;
         tabs.forEach((v,i) => {
           i==index?v.isActive=true:v.isActive=false
         });
        this.setData({
            index,
            tabs
         })
    },
    // 页面切换
    changePage: function (e) {
        const { index } = e.currentTarget.dataset;
        //修改源数组
        let {tabs} =this.data;
        tabs.forEach((v,i) => {
          i==index?v.isActive=true:v.isActive=false
        });
       this.setData({
           index,
           tabs
        })
    },
    // 实现播放器播放功能
    audioCtx: null,
    onReady: function() {
        this.audioCtx = wx.createInnerAudioContext()
        // 默认选择第1曲
        this.setMusic(0)
        var that = this
        // 播放进度检测
        this.audioCtx.onError(function() {
            console.log('播放失败：' + that.audioCtx.src)
        })
        // 播放完成自动换下一曲
        this.audioCtx.onEnded(function() {
            that.next()
        })
        // 自动更新播放进度
        this.audioCtx.onPlay(function() {})
        this.audioCtx.onTimeUpdate(function() {
            that.setData({
            'play.duration': formatTime(that.audioCtx.duration),
            'play.currentTime': formatTime(that.audioCtx.currentTime),
            'play.percent': that.audioCtx.currentTime / that.audioCtx.duration * 100
            })
        })
        // 格式化时间
        function formatTime(time) {
            var minute = Math.floor(time / 60) % 60;
            var second = Math.floor(time) % 60
            return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
        }
    },
    // 音乐播放
    setMusic: function(index) {
        var music = this.data.playlist[index]
        this.audioCtx.src = music.src
        this.setData({
            playIndex: index,
            'play.title': music.title,
            'play.singer': music.singer,
            'play.coverImgUrl': music.coverImgUrl,
            'play.currentTime': '00:00',
            'play.duration': '00:00',
            'play.percent': 0
        })
    },

    // 播放按钮
    play: function() {
        this.audioCtx.play()
        this.setData({
            state: '正在播放'
        })
    },

    // 暂停按钮
    pause: function() {
        this.audioCtx.pause()
        this.setData({
            state: '暂停中'
        })
    },

    // 下一曲按钮
    next: function() {
        var index = this.data.playIndex >= this.data.playlist.length - 1 ? 0 : this.data.playIndex + 1
        this.setMusic(index)
        if (this.data.state === '正在播放') {
            this.play()
        }
        this.audioCtx.play()
        this.setData({
            state: '正在播放'
        })
    },

    // 滚动条调节歌曲进度
    sliderChange: function(e) {
        var second = e.detail.value * this.audioCtx.duration / 100
        this.audioCtx.seek(second)
    },

    // 播放列表换曲功能
    change: function (e) {
        const {index} = e.currentTarget.dataset
        if (this.data.state === '正在播放' && this.data.playIndex === index) {
            // this.setMusic(index)
            this.pause()
            return
        }
        this.setMusic(index)
        this.play()
    },
    changeSong: function (e) {
        const {index} = e.currentTarget.dataset
        if (this.data.state === '正在播放' && this.data.playIndex === index) {
            // this.setMusic(index)
            this.pause()
            return
        }
        this.setMusic(index)
        this.play()
    }
});  